package com.sctu.edu.tracks;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.edu.tracks.bean.PoiBean;
import com.sctu.edu.tracks.config.URLConfig;
import com.sctu.edu.tracks.utils.L;
import com.sctu.edu.tracks.utils.httputils.GetVolleyHttp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengsenwen on 2017/5/25.
 */
public class RecordDetailsActivity extends Activity {

    private MapView mapView;
    private AMap aMap;
    private long recordId;
    private TextView title;
    private String recordName;
    private Activity activity;
    private List<PoiBean.Data> list;
    private List<LatLng> latLngs = new ArrayList<LatLng>();
    private Polyline polyLine;
    private MyLocationStyle myLocationStyle;
    private Button back;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details);

        recordId = Long.parseLong(getIntent().getExtras().getString("recordId"));
        recordName = getIntent().getExtras().getString("recordNameStr");
        L.e("det", recordId + "    " + recordName);

        title = (TextView) findViewById(R.id.activity_details_title);
        String titleName = recordName.substring(0, recordName.length() - 36);
        title.setText(titleName + "的足迹");
        back = (Button) findViewById(R.id.activity_details_back);

        //获取地图控件引用
        mapView = (MapView) findViewById(R.id.activity_details_map);
        mapView.onCreate(savedInstanceState);

        //初始化地图控制器对象
        if (aMap == null) {
            aMap = mapView.getMap();
        }
        myLocationStyle = new MyLocationStyle();
        myLocationStyle.radiusFillColor(Color.WHITE);
        aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_ROTATE);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        requestPois();
    }

    private void requestPois() {
        final Response.Listener<String> listener = new Response.Listener<String>() {
            @Override
            public void onResponse(String json) {
                L.e("poiJson", json);
                PoiBean poiBean = JSON.parseObject(json, PoiBean.class);
                list = poiBean.getData();

                for (int i = 0; i < list.size(); i++) {
                    latLngs.add(new LatLng(list.get(i).getLatitude(), list.get(i).getLongitude()));
                }

                //绘制路线
                polyLine = aMap.addPolyline(new PolylineOptions().
                        addAll(latLngs).width(30).color(Color.argb(255, 1, 1, 1)));

                //让地图显示指定位置
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLngs.get(0), 17));
                //设置起点和终点
                aMap.addMarker(new MarkerOptions().position(latLngs.get(0)).title("起点").snippet("DefaultMarker"));
                aMap.addMarker(new MarkerOptions().position(latLngs.get(latLngs.size() - 1)).title("终点").snippet("DefaultMarker"));
            }
        };
        Response.ErrorListener errorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                L.e("poi", volleyError.getMessage() + "");
            }
        };

        GetVolleyHttp.getInstance(this).getStringGETJsonData(URLConfig.getRecordPoisById(recordId),
                listener, errorListener, (long) 5000);
    }

}
